import os
import wmi
import time
import win32con
import win32api
import win32process
from PyQt4 import QtGui

#
# These functions are included in wmi from 1.0rc4
#

def createWidget():
    hello = QtGui.QPushButton("Hello world!")
    hello.resize(100, 30)
    hello.show()


def str_or_stars (i, length):
  if i is None:
    return "*" * length
  else:
    return str (i).rjust (length, "0")

def from_time (
  year=None, 
  month=None, 
  day=None, 
  hours=None, 
  minutes=None, 
  seconds=None, 
  microseconds=None, 
  timezone=None
):
    
  """Returns a WMI time string of the form yyyymmddHHMMSS.mmmmmm+UUU
  replacing each placeholder by its respective integer value, or
  stars if None is supplied
  """
  wmi_time = ""
  wmi_time += str_or_stars (year, 4)
  wmi_time += str_or_stars (month, 2)
  wmi_time += str_or_stars (day, 2)
  wmi_time += str_or_stars (hours, 2)
  wmi_time += str_or_stars (minutes, 2)
  wmi_time += str_or_stars (seconds, 2)
  wmi_time += "."
  wmi_time += str_or_stars (microseconds, 6)
  wmi_time += str_or_stars (timezone, 4)
  
  return wmi_time


def progTask():
    c = wmi.WMI ()
    
    year, month, day, hours, mins, secs = time.gmtime ()[:6]
    job_id, result = c.Win32_ScheduledJob.Create (
      Command=r"cmd.exe /c dir /b c:\ > c:\\temp.txt",
      StartTime=from_time (
        hours=hours, 
        minutes=mins+1, 
        seconds=secs, 
        microseconds=0, 
        timezone="+000"
      )
    )
    print job_id
    
    for line in os.popen ("at"):
      print line
      
def progexec():
     c = wmi.WMI ("localhost")
     startup = c.Win32_Process.Create(CommandLine="notepad.exe")
     
      
def killprocess():
    the_world_is_flat=1
    if the_world_is_flat:
        print "Be careful dddd"

    print sys.version

    #import os
    #print os.spawnl(os.P_NOWAIT, "c:\\winnt\\system32\\notepad.exe")
    
    while 1:
        c = wmi.WMI()
        
        diskList = [x for x in c.Win32_LogicalDisk (DriveType=3) ]
        procList = [proc for proc in  c.Win32_Process () ]
        
        for x in range(10):
            for disk in diskList:
                print disk
        
            for process in procList:
                print process.Caption
                if ( process.Caption.lower() == "filezilla.exe" ):
                    process.Terminate()
